package net.hhk.twitter;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.hhk.connect.HTTPConnect;
import net.hhk.controller.SuggestAdapter;
import net.hhk.data.Constant;
import net.hhk.data.MySharePreferences;
import net.hhk.data.User;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewProfileActivity extends Activity implements OnClickListener {
	public User user;
	private MySharePreferences sharePre;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		user = new User();
		setContentView(R.layout.view_profile_layout);
		sharePre = (MySharePreferences) getApplicationContext();
		getUserInfo();
		getTotalTweet();
		showProfile();
		//ReloadData();
		RelativeLayout tweets_layout =(RelativeLayout)findViewById(R.id.profile_tweets_layout);
		tweets_layout.setOnClickListener(this);
		RelativeLayout following_layout =(RelativeLayout)findViewById(R.id.profile_following_layout);
		following_layout.setOnClickListener(this);
		RelativeLayout follower_layout =(RelativeLayout)findViewById(R.id.profile_follower_layout);
		follower_layout.setOnClickListener(this);
		
		TextView switch_account =(TextView)findViewById(R.id.switch_account);
		switch_account.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.view_profile_layout, menu);
		return true;
	}

	public void getUserInfo() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.USERINFO, postParameters);
		try {
			JSONObject obj = new JSONObject(result);
			user.setUserid(obj.getInt("id"));
			user.setEmail(obj.getString("email"));
			user.setUsername(obj.getString("username"));
			user.setFullname(obj.getString("screenName"));
			user.setAvatar(obj.getString("avatar"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getTotalTweet() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.TOTALTWEET, postParameters);
		try {

			JSONObject obj = new JSONObject(result);
			user.setTweer_number(obj.getInt("totalTweet"));
			user.setFollower(obj.getInt("totalFollower"));
			user.setFollowing(obj.getInt("totalFollowing"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showProfile() {
		ImageView profile_pictue = (ImageView) findViewById(R.id.profile_picture);
		Object content = null;
		try {
			URL url = new URL(user.getAvatar());
			content = url.getContent();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		InputStream is = (InputStream) content;
		Drawable imagine = Drawable.createFromStream(is, "src");
		// Image01.setImageDrawable(image);
		profile_pictue.setImageDrawable(imagine);
		TextView fullname = (TextView) findViewById(R.id.profile_fullname);
		fullname.setText(user.getFullname());
		TextView tweet = (TextView) findViewById(R.id.profile_tweets);
		tweet.setText(Integer.toString(user.getTweer_number()));
		TextView follower = (TextView) findViewById(R.id.profile_follower_1);
		follower.setText(Integer.toString(user.getFollower()));

		TextView following = (TextView) findViewById(R.id.profile_following);
		following.setText(Integer.toString(user.getFollowing()));
		
		RelativeLayout profile = (RelativeLayout) findViewById(R.id.profile_view);
		profile.setOnClickListener(this);
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.profile_tweets_layout:
			Intent i = new Intent(getApplicationContext(), MyTweetActivity.class);
			Bundle tweet = new Bundle();
			tweet.putInt("id", 0);
			i.putExtras(tweet);
			startActivity(i);
			
			break;
		case R.id.profile_following_layout:
			Intent following = new Intent(getApplicationContext(), UserFollowActivity.class);
			Bundle b = new Bundle();
			b.putBoolean("isfollowing", true);
			following.putExtras(b);
			startActivity(following);
			//finish();
			break;

		
		case R.id.profile_follower_layout:
			Intent follower = new Intent(getApplicationContext(), UserFollowActivity.class);
			Bundle followe = new Bundle();
			followe.putBoolean("isfollowing", false);
			follower.putExtras(followe);
			startActivity(follower);
		//	finish();
			break;
		case R.id.profile_view:
			Intent toUserProfile = new Intent(getApplicationContext(),
					UserProfileActivity.class);
			Bundle toBundle = new Bundle();
			toBundle.putInt("id",user.getUserid() );
			
			toUserProfile.putExtras(toBundle); // Put your id to your next Inten
			startActivity(toUserProfile);
			sharePre.setExitString("currentVisitUsername", user.getUsername());
			break;
		case R.id.switch_account:
			Intent ik = new Intent(getApplicationContext(), LoginActivity.class);
			ik.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(ik);
			finish();

			break;
		default:
			break;
		}
	}
	public void ReloadData(){
		AsyncTask<String, Void, User > asyncTask = new AsyncTask<String, Void, User>() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPreExecute()
			 */
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
			}

			@Override
			protected User doInBackground(String... params) {
				// Following actions will be done in background
				getUserInfo();
				getTotalTweet();
				return user;
				
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
			 */
			@Override
			protected void onPostExecute(User result) {
				super.onPostExecute(result);
				showProfile();

			}
		};
		asyncTask.execute("Saurabh");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ReloadData();
	}
}
